package net.xi.news.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xi.news.NotFoundException;
import net.xi.news.dao.NewsMapper;
import net.xi.news.dao.SysUserMapper;
import net.xi.news.dao.UserMapper;
import net.xi.news.pojo.News;
import net.xi.news.pojo.NewsQuery;
import net.xi.news.pojo.SysUser;
import net.xi.news.pojo.User;
import net.xi.news.service.NewsService;
import net.xi.news.service.RedisService;
import net.xi.news.util.MarkdownUtils;
import net.xi.news.util.MyBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.*;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper,News> implements NewsService {


    @Autowired
    NewsMapper newsRespository;

    @Autowired
    UserMapper userRespository;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisService redisService;

    @Override
    //根据id得到新闻
    public News getNews(String id) {
        return newsRespository.selectById(id);
    }

    @Override
    //分页，列出所有新闻
    public Page<News> listNews(Integer page) {
        LambdaQueryWrapper<News> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(News::getStatus,"1");
        queryWrapper.orderByDesc(News::getCreateTime);
        Page<News> objectPage = new Page<>(page,10);
        Page<News> newsPage = newsRespository.selectPage(objectPage, queryWrapper);
        return newsPage;
    }

    @Override
    //根据id得到news，进行模糊查询
    public Page<News> listNews(Pageable pageable, NewsQuery news) {
        Page<News> page=new Page<>();
        page.setSize(pageable.getPageSize());
        page.setCurrent(pageable.getPageNumber());
        QueryWrapper<News> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(news.getTitle())){
            queryWrapper.like("title",news.getTitle());
        }
        if (!StringUtils.isEmpty(news.getTypeId())){
            queryWrapper.eq("typeid",news.getTypeId());
        }
        if (news.isRecommend()){
            queryWrapper.eq("recommend",news.isRecommend());
        }
        return newsRespository.selectPage(page,queryWrapper);

    }

    @Transactional
    @Override
    //保存新闻
    public News saveNews(News news) {
        if (news.getId() == null){
            news.setCreateTime(new Date());
            news.setUpdateTime(new Date());
            news.setViews(0);
        }else {
            news.setUpdateTime(new Date());
        }


        int insert = newsRespository.insert(news);
        return news;
    }


    @Override
    //分页列出查询得到的新闻
    public Page<News> listsearchNews(String query, Pageable pageable) {
        Page<News> page=new Page<>();
        page.setCurrent(pageable.getPageNumber()+1);
        page.setSize(10);
        QueryWrapper<News> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(query)){
            queryWrapper.like("title",query);
        }
        queryWrapper.like("status",1);
        return newsRespository.selectPage(page,queryWrapper);
    }

    @Override
    //列出前？新闻
    public List<News> newsTop(Integer size) {
        String top = redisService.get("NEWS_TOP5_NEWS");
        if (StringUtils.isEmpty(top)){
            Sort.Order order=new Sort.Order(Sort.Direction.DESC, "updateTime");
            Pageable pageable =  PageRequest.of(0,size,Sort.by(order));
            Page<News> page=new Page<>();
            page.setSize(pageable.getPageSize());
            page.setCurrent(pageable.getPageNumber());
            LambdaQueryWrapper<News> queryWrapper=Wrappers.lambdaQuery();
            queryWrapper.orderByDesc(News::getViews);
            Page<News> page1 = newsRespository.selectPage(page, queryWrapper);
            int i=1;
            for (News record : page1.getRecords()) {
                record.setCont(i+" ");
                i++;
            }
            String s = JSON.toJSONString(page1.getRecords());
            redisService.set("NEWS_TOP5_NEWS",60*30,s);
            return page1.getRecords();
        }else {
            List<News> newsList = JSON.parseArray(top, News.class);
            return newsList;
        }

    }

    @Transactional
    @Override
    //更新新闻
    public News updateNews(String id, News news) {
        News news1 = newsRespository.selectById(id);
        if (news1 == null){
            throw  new NotFoundException("该新闻不存在");
        }
        BeanUtils.copyProperties(news,news1, MyBeanUtils.getNullPropertyNames(news));
        news1.setUpdateTime(new Date());
        int insert = newsRespository.insert(news1);
        return news;
    }

    @Transactional
    @Override
    //删除新闻
    public void deleteNews(String id) {
        newsRespository.deleteById(id);

    }


    @Override
    //得到新闻详情内容
    public News getConvert(String id) {
        LambdaQueryWrapper<News> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(News::getStatus,"1").eq(News::getId,id);
        News news = newsRespository.selectOne(queryWrapper);
        if (news == null){
            throw new  NotFoundException("该新闻不存在");
        }
//        newsRespository.updateViews(id);
        return news;
    }

    @Override
    //根据标签列出新闻
    public Page<News> listNewsByTag(String tagId, Pageable pageable) {
        Page<News> page=new Page<>();
        page.setSize(pageable.getPageSize());
        page.setCurrent(pageable.getPageNumber());
        QueryWrapper<News> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(tagId)){
            queryWrapper.eq("tagIds",tagId);
        }
        return newsRespository.selectPage(page,queryWrapper);
    }


    @Override
    //根据月份进行分类，列出新闻
    public Map<String, List<News>> holeNews() {
        return null;
    }

    @Override
    //统计新闻数量
    public Integer countNews() {
        Integer integer = newsRespository.selectCount(null);
        return integer;
    }
}
